import tkinter as tk
import pywinauto
import tkinter.messagebox as messagebox
from time import sleep, time
from threading import Timer
import random, json, pyperclip, os, pyautogui, win32gui, win32con


class KeydownBatController:
  def __init__(self, tk_root) -> None:
    self.tk_root = tk_root
    self.build_ui()
    self.start_keydown_8_timer = None
    self.start_keydown_9_timer = None
    self.start_keydown_f1_timer = None
    self.start_keydown_f2_timer = None
    self.timer_focus_wow = None

  def build_ui(self):
    tk.Button(self.tk_root, text="按键8", command=self.run_keydown_8_task).place(x=10, y=130, width=80, height=30)
    tk.Button(self.tk_root, text="按键9", command=self.run_keydown_9_task).place(x=100, y=130, width=80, height=30)
    tk.Button(self.tk_root, text="按键f1", command=self.run_keydown_f1_task).place(x=190, y=130, width=80, height=30)
    tk.Button(self.tk_root, text="按键f2", command=self.run_keydown_f2_task).place(x=280, y=130, width=80, height=30)
    tk.Button(self.tk_root, text="停止", command=self.stop_all_task).place(x=370, y=130, width=80, height=30)

  def run_keydown_8_task(self):
    self.run_focus_wow()
    self.start_keydown_8_timer_func()
  
  def run_keydown_9_task(self):
    self.run_focus_wow()
    self.start_keydown_9_timer_func()

  def run_keydown_f1_task(self):
    self.run_focus_wow()
    self.start_keydown_f1_timer_func()

  def run_keydown_f2_task(self):
    self.run_focus_wow()
    self.start_keydown_f2_timer_func()

  def run_focus_wow(self):
    self.stop_focus_wow()
    wow_handle = win32gui.FindWindow(None, '魔兽世界')
    if not wow_handle:
      wow_handle = win32gui.FindWindow(None, '魔獸世界')
    win32gui.SetForegroundWindow(wow_handle)
    self.timer_focus_wow = Timer(random.uniform(5, 10), self.run_focus_wow)
    self.timer_focus_wow.start()

  def stop_focus_wow(self):
    if self.timer_focus_wow:
      self.timer_focus_wow.cancel()
      self.timer_focus_wow = None
    
  def start_keydown_8_timer_func(self):
    self.stop_keydown_8_timer_func()
    pyautogui.press("8")
    self.start_keydown_8_timer = Timer(random.uniform(1.1, 2.4), self.start_keydown_8_timer_func)
    self.start_keydown_8_timer.start()

  def stop_keydown_8_timer_func(self):
    if self.start_keydown_8_timer:
      self.start_keydown_8_timer.cancel()
      self.start_keydown_8_timer = None
  
  def start_keydown_9_timer_func(self):
    self.stop_keydown_9_timer_func()
    pyautogui.press("9")
    self.start_keydown_9_timer = Timer(random.uniform(1.1, 2.4), self.start_keydown_9_timer_func)
    self.start_keydown_9_timer.start()

  def stop_keydown_9_timer_func(self):
    if self.start_keydown_9_timer:
      self.start_keydown_9_timer.cancel()
      self.start_keydown_9_timer = None

  def start_keydown_f1_timer_func(self):
    self.stop_keydown_f1_timer_func()
    pyautogui.press("f1")
    self.start_keydown_f1_timer = Timer(random.uniform(1.1, 2.4), self.start_keydown_f1_timer_func)
    self.start_keydown_f1_timer.start()

  def stop_keydown_f1_timer_func(self):
    if self.start_keydown_f1_timer:
      self.start_keydown_f1_timer.cancel()
      self.start_keydown_f1_timer = None
  
  def start_keydown_f2_timer_func(self):
    self.stop_keydown_f2_timer_func()
    pyautogui.press("f2")
    self.start_keydown_f2_timer = Timer(random.uniform(1.1, 2.4), self.start_keydown_f2_timer_func)
    self.start_keydown_f2_timer.start()

  def stop_keydown_f2_timer_func(self):
    if self.start_keydown_f2_timer:
      self.start_keydown_f2_timer.cancel()
      self.start_keydown_f2_timer = None

  def stop_all_task(self):
    self.stop_keydown_8_timer_func()
    self.stop_keydown_9_timer_func()
    self.stop_keydown_f1_timer_func()
    self.stop_keydown_f2_timer_func()
    self.stop_focus_wow()
    